package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.SysIpBo;
import com.ruoyi.system.domain.vo.SysIpVo;
import com.ruoyi.system.service.ISysIpService;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ip设置
 *
 * @author ruoyi
 * @date 2023-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/ip")
public class SysIpController extends BaseController {

    private final ISysIpService iSysIpService;

    /**
     * 查询ip设置列表
     */
    @SaCheckPermission("system:ip:list")
    @GetMapping("/list")
    public TableDataInfo<SysIpVo> list(SysIpBo bo, PageQuery pageQuery) {
        return iSysIpService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出ip设置列表
     */
    @SaCheckPermission("system:ip:export")
    @Log(title = "ip设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysIpBo bo, HttpServletResponse response) {
        List<SysIpVo> list = iSysIpService.queryList(bo);
        ExcelUtil.exportExcel(list, "ip设置", SysIpVo.class, response);
    }

    /**
     * 获取ip设置详细信息
     *
     * @param infoId 主键
     */
    @SaCheckPermission("system:ip:query")
    @GetMapping("/{infoId}")
    public R<SysIpVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long infoId) {
        return R.ok(iSysIpService.queryById(infoId));
    }

    /**
     * 新增ip设置
     */
    @SaCheckPermission("system:ip:add")
    @Log(title = "ip设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysIpBo bo) {
        return toAjax(iSysIpService.insertByBo(bo));
    }

    /**
     * 修改ip设置
     */
    @SaCheckPermission("system:ip:edit")
    @Log(title = "ip设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysIpBo bo) {
        return toAjax(iSysIpService.updateByBo(bo));
    }

    /**
     * 删除ip设置
     *
     * @param infoIds 主键串
     */
    @SaCheckPermission("system:ip:remove")
    @Log(title = "ip设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] infoIds) {
        return toAjax(iSysIpService.deleteWithValidByIds(Arrays.asList(infoIds), true));
    }


    @PutMapping("/changeIp")
    public R<Void> changeIp(@RequestBody SysIpBo bo) {
        iSysIpService.changeIp(bo);
        return R.ok();
    }
}
